package com.woniuxy.activiti.controller;

import com.woniuxy.activiti.service.ActFlowCommService;
import com.woniuxy.activiti.service.EnterpriseCreditService;
import com.woniuxy.activiti.service.FlowService;
import com.woniuxy.entity.BorrowDemand;
import com.woniuxy.entity.EnterpriseCredit;
import com.woniuxy.entity.FlowInfo;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import com.woniuxy.utils.JWTUtil;
import com.woniuxy.service.business.BorrowDemandService;
import com.woniuxy.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 流程管理
 * @author xilei
 * @create 2021-11-12 16:40
 */
@RestController
public class FlowController {

    @Autowired
    private FlowService flowService;
    @Autowired
    private ActFlowCommService actFlowCommService;
    @Autowired
    private UserService userService;
    @Autowired
    private EnterpriseCreditService enterpriseCreditService;


    /**
     * 查询所有流程
     * @return
     */
    @GetMapping("/flow/findAll")
    public List<FlowInfo> findAllFlow(){
        return flowService.findAllFlow();
    }

    /**
     * 流程部署
     * @param id
     * @return  0-部署失败   1-部署成功   2-已经部署了
     *
     */
//    @PutMapping("/flow/deployment/{id}")
    @GetMapping("/flow/deployment/{id}")
    public Integer deployment(@PathVariable(name = "id") String id){
        System.out.println("------------"+id);
        FlowInfo flowInfo = flowService.findOneFlow(id);
        if (flowInfo.getState() == 0){
            return 2;
        }
        actFlowCommService.saveNewDeploy(flowInfo);
        return flowService.updateDeployState(id);
    }

    /**
     * 查询用户任务
     * @param request
     * @return
     */
    @GetMapping("/flow/findUserTask")
    public List<Map<String,Object>> findUserTask(HttpServletRequest request){
        String authentication = request.getHeader("authentication");
        String uname = JWTUtil.getUname(authentication);
        User user = userService.getUser(uname);
        int userId = user.getId();
        System.out.println("--===++++++");
        return flowService.findUserTask(userId);
    }

    /**
     * 查询任务详细信息
     * @param request
     * @return
     */
    @GetMapping("/flow/findTaskInfo")
    public List<Map<String,Object>> findTaskInfo(HttpServletRequest request){
        String authentication = request.getHeader("authentication");
        String uname = JWTUtil.getUname(authentication);
        User user = userService.getUser(uname);
        int userId = user.getId();
        System.out.println("-===++++++userId"+userId);
        List<Map<String, Object>> tasklist = flowService.findTaskInfo(userId);
        System.out.println(tasklist);
        return tasklist;
    }

    /**
     * 完成任务
     * @param request
     * @param taskId
     */
    @PutMapping("/flow/completeTask/{taskId}")
    public Boolean completeTask(HttpServletRequest request,
                             @PathVariable("taskId") String taskId){
        System.out.println("taskId"+taskId);
        String authentication = request.getHeader("authentication");
        String uname = JWTUtil.getUname(authentication);
        User user = userService.getUser(uname);
        int userId = user.getId();
        flowService.completeTask(taskId, userId);

        if(userId==3){

            EnterpriseCredit enterpriseCredit = enterpriseCreditService.findCreditById(1);
            enterpriseCreditService.updateCredit(true, "同意");
        }
        return true;
    };

    /**
     * 查询
     * @param id
     * @return
     */
    @GetMapping("/flow/findFlowTask/{id}")
    public Map<String,Object> findFlowTask(@PathVariable(name = "id") int id){
        String businessKey = "enterpriseCredit:"+id;
        actFlowCommService.searchHistoty(businessKey);
        return null;
    }
}
